import java.util.*;

public class BinaryTree {
    class TreeNode {
        public char val;
        public TreeNode left;//左孩子的引用
        public TreeNode right;//右孩子的引用

        public TreeNode(char val) {
            this.val = val;
        }
    }


    /**
     * 创建一棵二叉树 返回这棵树的根节点
     *
     * @return
     */
    public TreeNode createTree() {
        TreeNode root = new TreeNode('A');
        root.left = new TreeNode('B');
        root.right = new TreeNode('C');
        root.left.left = new TreeNode('D');
        root.left.right = new TreeNode('F');
        root.left.right.right = new TreeNode('G');
        root.right.left = new TreeNode('E');
        //root.left.right = new TreeNode('E');
        return root;
    }

    // 前序遍历
    public void preOrder(TreeNode root) {
        if(root == null){
            return;
        }
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }

    // 中序遍历
    void inOrder(TreeNode root) {
        if(root == null){
            return;
        }

        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }

    // 后序遍历
    void postOrder(TreeNode root) {
        if(root == null){
            return;
        }

        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val + " ");
    }

    int nodeSize = 0;

    /**
     * 获取树中节点的个数：遍历思路
     */
    void size(TreeNode root) {
        if(root == null){
            return;
        }
        nodeSize++;
        size(root.left);
        size(root.right);
    }

    /**
     * 获取节点的个数：子问题的思路
     *
     * @param root
     * @return
     */
    int size2(TreeNode root) {
        if(root == null){
            return 0;
        }
        return size2(root.left) + size2(root.right) + 1;
    }


    /*
     获取叶子节点的个数：遍历思路
     */
    int leafSize = 0;

    void getLeafNodeCount1(TreeNode root) {
        if(root == null){
            return;
        }
        if(root.left == null && root.right == null){
            leafSize++;
        }
        getLeafNodeCount1(root.left);
        getLeafNodeCount1(root.right);
    }

    /*
     获取叶子节点的个数：子问题
     */
    int getLeafNodeCount2(TreeNode root) {
        if(root == null){
            return 0;
        }
        if(root.right == null && root.left == null){
            return 1;
        }
        return getLeafNodeCount2(root.left) + getLeafNodeCount2(root.right);
    }

    /*
    获取第K层节点的个数
     */
    int getKLevelNodeCount(TreeNode root, int k) {
        if(root == null || k < 1){
            return 0;
        }
        if(k == 1){
            return 1;
        }

        return getKLevelNodeCount(root.left, k-1) + getKLevelNodeCount(root.right, k-1);
    }

    /*
     获取二叉树的高度
     时间复杂度：O(N)
     */
    int getHeight(TreeNode root) {
        if(root == null){
            return 0;
        }
        return Math.max(getHeight(root.left) , getHeight(root.right)) +1;
    }


    // 检测值为value的元素是否存在
    TreeNode ret;
    TreeNode find(TreeNode root, char val) {
        if(root == null){
            return root;
        }
        if(root.val == val){
            return ret = root;
        }
        find(root.left, val);
        find(root.right, val);
        return ret;
    }

    //层序遍历
    Queue<TreeNode> qu = new LinkedList<>();
    void levelOrder(TreeNode root) {
        TreeNode cur = root;
        if (root == null){
            return;
        }
        qu.offer(cur);
        System.out.print(qu.poll().val + " ");
        while(cur != null){
            if(cur.left != null){
                qu.offer(cur.left);
            }
            if(cur.right != null){
                qu.offer(cur.right);
            }
            if(qu.isEmpty()){
                break;
            }
            cur = qu.poll();
            System.out.print(cur.val + " ");
        }

    }

    // 判断一棵树是不是完全二叉树
    boolean isCompleteTree(TreeNode root) {
        if (root == null){
            return true;
        }
        if(root.left == null && root.right == null){
            return true;
        }
        if(root.left != null && root.right == null){
            return true;
        }
        if(root.left == null && root.right != null){
            return false;
        }
        return isCompleteTree(root.left) && isCompleteTree(root.right);
    }

    /*public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root == null){
            return null;
        }

        if(root == p || root == q){
            return root;
        }

        if((root.left == p && root.right == q) || (root.left == q && root.right == p) ){
            return root;
        }

        TreeNode leftTree = lowestCommonAncestor(root.left, p ,q);
        TreeNode rightTree = lowestCommonAncestor(root.right, p ,q);

        if(leftTree != null && rightTree != null){
            return root;
        }
        if(leftTree != null){
            return leftTree;
        }
        if(rightTree != null){
            return rightTree;
        }
        if(leftTree == rightTree && leftTree == null){
            return null;
        }

        return root;
    }*/
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root == null){
            return null;
        }
        Stack<TreeNode> stack1 = new Stack<>();
        Stack<TreeNode> stack2 = new Stack<>();

        findPath(stack1, root, p);
        findPath(stack2, root, q);

        int size = stack1.size()-stack2.size();

        if(size < 0){
            size = -size;
            while(size > 0){
                stack2.pop();
                size--;
            }
        }else{
            while (size > 0){
                stack1.pop();
                size--;
            }
        }

        while(!stack1.isEmpty()){
            if(stack1.peek() == stack2.peek()){
                return stack1.pop();
            }
            stack1.pop();
            stack2.pop();
        }
        return null;
    }

    private boolean findPath(Stack<TreeNode> stack,TreeNode cur, TreeNode node){
        if(cur == null){
            return false;
        }
        stack.push(cur);
        if(cur == node){
            return true;
        }

        boolean ret1 = findPath(stack, cur.left, node);
        boolean ret2 = findPath(stack, cur.right, node);

        if(ret1 == false && ret2 == false){
            stack.pop();
        }

        return ret1 || ret2;

    }

    public List<List<Character>> levelOrderBottom(TreeNode root) {
        List<List<Character>> listList = new ArrayList<>();
        if(root == null){
            return listList;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        TreeNode cur = root;

        while (!queue.isEmpty()){
            /*if(cur.left != null){
                queue.offer(cur.left);
            }
            if(cur.right != null){
                queue.offer(cur.right);
            }*/
            int size = queue.size();
            List<Character> list1 = new ArrayList<>();
            while(size > 0){
                cur = queue.poll();
                if(cur.left != null){
                    queue.offer(cur.left);
                }
                if(cur.right != null){
                    queue.offer(cur.right);
                }
                size--;
                list1.add(cur.val);
            }
            listList.add(0,list1);
        }
        return listList;
    }

    public List<Character> postorderTraversal(TreeNode root) {
        Stack<TreeNode> stack = new Stack<>();
        List<Character> list = new ArrayList<>();
        TreeNode cur = root;
        if(root == null){
            return list;
        }
        if(root.left == null && root.right == null){
            list.add(root.val);
            return list;
        }

        while(cur != null || !stack.isEmpty()) {
            while (cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            TreeNode top = stack.pop();
            list.add(top.val);
            cur = top.right;
        }
        return list;
    }
}